package com.sx.pipeline.adapter

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader
import com.sx.mvvmlibs.base.EasyAdapter
import com.sx.pipeline.R

/**
 * author : zhouyouqiang
 * e-mail : 1638473898@qq.com
 * date   : 2022/4/11 15:59
 * desc   : String适配器
 * version: 1.0
 */
class ReleaseImageAdapter(
    private val contexts: Context,
    private val releaseType: List<String>,
) : EasyAdapter<String,ReleaseImageViewHolder?>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReleaseImageViewHolder {
        val view = View.inflate(contexts, R.layout.item_release_image, null)
        return ReleaseImageViewHolder(view)
    }

    override fun getItemCount(): Int {
        return releaseType.size
    }

    override fun whenBindViewHolder(holder: ReleaseImageViewHolder?, position: Int) {
        val data = releaseType[position]
        holder!!.ivImg.tag = position //绑定
        Glide.with(contexts).load(data).into(holder.ivImg)
        holder.ivImg.setOnClickListener {
            XPopup.Builder(contexts)
                .isDestroyOnDismiss(true)
                .asImageViewer(
                    holder.ivImg,
                    data,
                    false,
                    Color.parseColor("#f1f1f1"),
                    -1,
                    0,
                    true,
                    Color.BLACK,
                    SmartGlideImageLoader(com.luck.picture.lib.R.drawable.ps_image_placeholder)
                ) { _, position ->
                    Toast.makeText(
                        contexts,
                        "长按了第" + position + "个图片",
                        Toast.LENGTH_SHORT
                    ).show()
                }
                .show()
        }
    }
}

class ReleaseImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var ivImg: ImageView = itemView.findViewById(R.id.iv_img)
}